/**
 * Created by taohj on 2014/5/21.
 */

var user = function () {
};


/**
 * 模块列表
 * */
user.initgrid = function (id) {
    user.datagrid=$("#sys_user_list_datagrid");
    user.datagrid.datagrid({
        url: functions.get("SysUserList"),
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        sortName:"id",
        sortOrder:"asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,
        onDblClickRow:function(){
            var options=user.datagrid.datagrid("options");
            options.toolbar[2].handler();
        },
        toolbar: [{
            iconCls: 'icon-add',
            text:'Create',
            handler: function(){
                var createDialog = $("<div></div>");
                createDialog.dialog({
                    title: 'SysUserCreate',
                    href:  functions.get("SysUserCreateView"),
                    modal: true,
                    width: 280,
                    top:100
                });
            }
        },'-',{
            iconCls: 'icon-edit',
            text:'Modify',
            handler: function(){

                var selectRow= user.datagrid.datagrid("getSelected");
                if(!selectRow){
                    //todo
                    return;
                }

                var id="";
                if(selectRow){
                    id=selectRow.id;
                }

                var createDialog = $("<div></div>");
                createDialog.dialog({
                    title: 'SysUserModify',
                    href: functions.get("SysUserModifyView")+'?id='+id,
                    modal: true,
                    width: 280,
                    top:100
                });
            }
        },'-',{
            iconCls: 'icon-remove',
            text:'Remove',
            handler: function(){

                var selectRow= user.datagrid.datagrid("getSelected");
                if(!selectRow){
                    //todo
                    return;
                }

                $.messager.confirm('Confirm', 'Are you sure remove selected object?', function(r){
                    if (r){
                        $.post(functions.get("SysUserRemove"), {
                            id:selectRow.id
                        }, function (result) {
                            util.requestUtil(result,null,user.datagrid);
                        });
                    }
                });
            }
        },'-',{
            iconCls: 'icon-edit',
            text:'Assigned roles',
            handler: function(){
                var selectRow= user.datagrid.datagrid("getSelected");
                if(!selectRow){
                    //todo
                    return;
                }
                var id="";
                if(selectRow){
                    id=selectRow.id;
                }
                var dialog = $("<div></div>");
                dialog.dialog({
                    title: 'SysUserAssignedRoles',
                    href: functions.get("SysUserAssignedRolesView")+'?id='+id,
                    modal: true,
                    width: 600,
                    height:450,
                    top:50,
                    buttons:[{
                        text:'Save',
                        handler:function(){


                          var rows=user.assignedRolesDatagrid.datagrid('getChecked');
                          if((!rows) || rows.length==0){
                              //todo
                              return;
                          }
                          var p='';
                          for(var i=0;i<rows.length;i++){
                              p+='&roleid='+rows[i].id;
                          }
                            $.post(functions.get('SysUserAssignedRoles')+'?id='+id+p,{}, function (result) {
                                util.requestUtil(result,dialog,null);
                            });
                        }
                    },{
                        text:'Close',
                        handler:function(){
                            dialog.dialog('close');
                        }
                    }]

                });
            }
        }],
        columns:[[
            {field:"id",title:"ID",width:40,sortable:true},
            {field:"realName",title:"REALNAME",width:100,sortable:true},
            {field:"nickName",title:"NICKNAME",width:100,sortable:true},
            {field:"userName",title:"USERNAME",width:100,sortable:true},
            {field:"sex",title:"SEX",width:50,formatter:function(value){
                return util.decode(value,[[1,"male"],[0,"female"]],"unknow");

            },sortable:true},
            {field:"birthday",title:"BIRTHDAY",width:100,sortable:true},
            {field:"address",title:"ADDRESS",width:250,sortable:true},
            {field:"phone",title:"PHONE",width:100,sortable:true},
            {field:"email",title:"EMAIL",width:150,sortable:true},
            {field:"qQ",title:"QQ",width:80,sortable:true},
            {field:"headImage",title:"HEADIMAGE",width:50,sortable:true},
            {field:"registrationTime",title:"REGISTRATIONTIME",width:80,sortable:true},
            {field:"lastloginTime",title:"LASTLOGINNAME",width:80,sortable:true},
            {field:"status",title:"STATUS",width:80,sortable:true,formatter:function(value){
                return util.decode(value,[[1,"normal"],[0,"disable"]],"unknown")
            }}

        ]]
    });
};




user.assignedRolesViewInit=function(id){


    user.assignedRolesDatagrid=$("#sys_user_assignedRoles_datagrid");
    user.assignedRolesDatagrid.datagrid({
        url: functions.get("SysUserAssignedRolesList")+'?id='+id,
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns:true,
        sortName:"id",
        sortOrder:"asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,
        onLoadSuccess:function(data){
            console.info(data);
            if(data){
                $.each(data.rows, function(index, item){
                    if(item.checked){
                        user.assignedRolesDatagrid.datagrid('checkRow', index);
                    }
                });
            }
        },

        onDblClickRow:function(rowIndex,rowData){


            var selectRow= user.assignedRolesDatagrid.datagrid("getSelected");
            if(!selectRow){
                //todo
                return;
            }
            var dialog=$("<div></div>");
            dialog.dialog({
                title: selectRow.name + '-->Authorize',
                href: functions.get('SysRoleAuthorizeView') + '?id=' + selectRow.id,
                modal: true,
                width: 500,
                height: 450,
                top: 50
            });
        },
        columns:[[
            {field:"checked",checkbox:true},
            {field:"id",title:"ID",width:60,sortable:true},
            {field:"name",title:"NAME",width:200,sortable:true},
            {field:"description",title:"DESCRIPTION"}
        ]]
    });
}






//新建提交
user.createSubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get("SysUserCreate"), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,user.datagrid)
        });
    }
};


//修改提交
user.modifySubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get("SysUserModify"), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,user.datagrid);
        });
    }
};

//检索
user.searchSubmit=function(button){
    var form =util.form(button);
    var params = util.serializeObject(form);
    user.datagrid.datagrid("load",params);
}


//bmi
user.assignedRolesSearchSut=function(button){
    var form =util.form(button);
    var params = util.serializeObject(form);
    user.assignedRolesDatagrid.datagrid("load",params);
}



